package cn.zjj.user.service.impl;

import cn.zjj.basic.constans.VerifyCodeConstants;
import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.basic.util.Md5Utils;
import cn.zjj.basic.util.StrUtils;
import cn.zjj.user.domain.Logininfo;
import cn.zjj.user.domain.User;
import cn.zjj.user.dto.UserPhoneRegisterDto;
import cn.zjj.user.mapper.LogininfoMapper;
import cn.zjj.user.mapper.UserMapper;
import cn.zjj.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    //添加
    @Override
    public void insert(User user){
        /**
         * 先添加logininfo,返回他的id   再添加user,就可以直接添加user  logininfo_id
         */

        //先把数据添加进logininfo对象,再调添加方法
       /* addLogininfo(user);*/
        //添加logininfo   返回了它的id
        logininfoMapper.insert(user.getLogininfo());
        //把logininfo_id字段补齐
        user.setLogininfo_id(user.getLogininfo().getId());
        //调用添加user方法
        userMapper.insert(user);

    }
    //后端方法做,目前用的前端方法(后端关联查logininfo所有,返回给前端,直接返回来赋过值的logininfo对象)
    /*public void addLogininfo(User user){
        Logininfo logininfo = user.getLogininfo();
        logininfo.setUsername(user.getUsername());
        logininfo.setPhone(user.getPhone());
        logininfo.setEmail(user.getEmail());
        logininfo.setSalt(user.getSalt());
        logininfo.setPassword(user.getPassword());
    }
*/
    //修改
    @Override
    public void update(User user){
        /**
         * 根据当前user的logininfo_id找到logininfo表对应的数据,并且更新logininfo表数据
         */
        //先把数据添加进logininfo对象,再调修改方法
        /*addLogininfo(user);*/
        //更新logininfo表数据
        logininfoMapper.update(user.getLogininfo());
        //更新user表数据
        userMapper.update(user);

    }

    @Override
    public void delete(Long id){
        try {
            //通过user的id查到user的Logininfo_id,删除logininfo表对应数据
            logininfoMapper.delete(userMapper.selectById(id).getLogininfo_id());
        } catch (Exception e) {
        }
        //删除user
        userMapper.delete(id);

    }

    @Override
    public void patchDel(List<Long> ids){
        try {
            List<Long> longs = new ArrayList<>();
            for (Long id : ids) {
                longs.add(userMapper.selectById(id).getLogininfo_id());
            }
            //批量删除logininfo
            logininfoMapper.patchDel(longs);
        } catch (Exception e) {
        }
        //批量删除user
        userMapper.patchDel(ids);

    }
    //用户手机注册
    @Override
    public void phoneRegister(UserPhoneRegisterDto userPhoneRegisterDto) {
        //判断空值
        String phone = userPhoneRegisterDto.getPhone();
        String phoneCode = userPhoneRegisterDto.getPhoneCode();
        String password = userPhoneRegisterDto.getPassword();
        String passwordRepeat = userPhoneRegisterDto.getPasswordRepeat();
        //1.校验 - 空值校验
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("数据不能为空");
        }

        //2.校验密码是否一致
        if(!password.trim().equals(passwordRepeat.trim())){
            throw new BusinessException("两次密码不一致");
        }

        //3.校验验证码是否过期 :   //会根据手机要获取验证码,所以不需要再判断是否注册,验证码哪儿已经验证了,始终过不了那儿
        Object redisPhoneCode = redisTemplate.opsForValue().get(VerifyCodeConstants.REGISTER_CODE_PREFIX + phone);
        if(redisPhoneCode == null){
            throw new BusinessException("手机验证码失效");
        }
        //4.校验验证码是否正确
        if(!redisPhoneCode.toString().split(":")[0].equalsIgnoreCase(phoneCode)){
            throw new BusinessException("手机验证码错误");
        }

        User user = setUserMsg(userPhoneRegisterDto);


        Logininfo logininfo = setLogininfoMsg(user);
        //权限信息添加到数据库 返回logininfo_id
        logininfoMapper.insert(logininfo);
        //用户信息添加到数据库
        user.setLogininfo_id(logininfo.getId());
        userMapper.insert(user);
    }

    //设置用户信息
    private User setUserMsg(UserPhoneRegisterDto userPhoneRegisterDto){
        User user = new User();
        //设置账号 用户加手机拼接
        user.setUsername("pethome"+userPhoneRegisterDto.getPhone());
        //设置手机号
        user.setPhone(userPhoneRegisterDto.getPhone());
        //设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //密码+盐值   md5加密
        String Md5Password = Md5Utils.encrypByMd5(userPhoneRegisterDto.getPassword() + salt);
        user.setPassword(Md5Password);
        return user;
    }
    //设置用户权限信息
    private Logininfo setLogininfoMsg(User user){
        Logininfo logininfo = new Logininfo();
        //如果两个类不一样时，只会拷贝属性一样的内容
        BeanUtils.copyProperties(user,logininfo);
        //设置权限
        logininfo.setType(1); //1是普通用户
        return logininfo;
    }
}
